package com.cy.springcloudalibaba.transaction.producer;

import com.cy.springcloudalibaba.source.MySource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

/**
 * @Description: 发送消息
 * @ClassName: SendService
 * @Author: chenyu
 * @Date: 2025/3/3 16:08
 */
@Service
public class SendService {

    @Autowired
    private MySource source;

    public <T> void sendTransactionalMsg(T msg, int num) throws Exception {
        MessageBuilder<T> builder = MessageBuilder.withPayload(msg)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .setHeader("test", String.valueOf(num));

        Message<T> message = builder.build();
        source.outputTX().send(message);
    }
}
